<template>
  <div class="m-content">
    <h3>欢迎来到cdd的博客</h3>
    <div class="block"><el-avatar :size="50" :src="user.avatar"></el-avatar></div>
    <div>{{ user.username }}</div>
    <div class="maction">
      <span><el-link href="/blogs" type="primary">主页</el-link></span>
      <el-divider direction="vertical"></el-divider>
      <span><el-link href="/blog/add" type="primary">发表文章</el-link></span>

      <el-divider direction="vertical"></el-divider>
      <span v-if="!hasLogin"><el-link type="info" href="/login">登录</el-link></span>
      <span v-if="hasLogin"><el-link type="danger" @click="logout">退出登录</el-link></span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Header',
  data: function () {
    return {
      user: {
        avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
        username: '请先登录'
      },
      hasLogin: false
    }
  },
  methods: {
    logout () {
      const _this = this
      _this.$axios.get('/logout', {
        headers: {
          'Authorization': localStorage.getItem('token')
        }
      }).then(res => {
        _this.$store.commit('REMOVE_INFO')
        _this.$router.push('/login')
      })
    }
  },
  created () {
    if (this.$store.getters.getUser.username) {
      this.user.username = this.$store.getters.getUser.username
      this.user.avatar = this.$store.getters.getUser.avatar
      this.hasLogin = true
    }
  }
}
</script>

<style scoped>
  .m-content {
    max-width: 960px;
    margin: auto;
    text-align: center;
  }
  .maction {
    margin: 10px;
  }

</style>
